package cn.usian.controller;

import cn.usian.common.R;
import cn.usian.pojo.Category;
import cn.usian.service.CategoryService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    //分页
    @GetMapping("/page")
    public R page(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "10")int pageSize){
        return categoryService.findPage(page,pageSize);
    }
    //添加
    @PostMapping
    public R save(@RequestBody Category category, HttpSession session){
        session.getAttribute("employee");
        Long id =(Long) session.getAttribute("employee");
        category.setCreateUser(id);
        category.setUpdateUser(id);
        categoryService.save(category);
        return R.success("添加成功");
    }
    //修改
    @PutMapping
    public R update(@RequestBody Category category,HttpSession session){
        Long id =(Long) session.getAttribute("employee");
        category.setUpdateUser(id);
        category.setUpdateTime(LocalDateTime.now());
        categoryService.update(category);
        return R.success("修改成功");
    }
    //删除
    @DeleteMapping
    public R del(Long id){
        categoryService.del(id);
        return R.success("删除成功");
    }
    //导出
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        //1. 读取模板的输入流
        InputStream inputStream = this.getClass().getResourceAsStream("/excel/cateory.xlsx");

        //2. 使用模块输入流创建工作簿
        Workbook workbook = new XSSFWorkbook(inputStream);

        //3. 得到工作单，得到第一行，并且提取样式存储到集合中
        Sheet sheet = workbook.getSheetAt(0);
        Row row = sheet.getRow(1);
        //定义一个集合存储单元格的样式
        List<CellStyle> cellStyleList = new ArrayList<>();
        for (int i = 1; i <=5 ; i++) {
            //得到每一个单元格的样式
            CellStyle cellStyle = row.getCell(i).getCellStyle();
            //存储到集合中
            cellStyleList.add(cellStyle);
        }
        //4. 得到所有的类别集合
        List<Category> categoryList = categoryService.findAll();

        //5. 遍历类别集合，设置样式与数据
        for (int i = 0; i < categoryList.size(); i++) {
            //得到一个类别，每一个类别就是一行数据
            Category category = categoryList.get(i);
            //创建行
            row = sheet.createRow(i+1);

            //创建单元格并且设置单元格
            //类别类型
            Cell cell = row.createCell(1);
            cell.setCellStyle(cellStyleList.get(0));
            cell.setCellValue(category.getType());

            //类别名称
            cell = row.createCell(2);
            cell.setCellStyle(cellStyleList.get(1));
            cell.setCellValue(category.getName());

            //排序
            cell = row.createCell(3);
            cell.setCellStyle(cellStyleList.get(2));
            cell.setCellValue(category.getSort());

            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

            //创建时间
            cell = row.createCell(4);
            cell.setCellStyle(cellStyleList.get(3));
            cell.setCellValue(dateTimeFormatter.format(category.getCreateTime()));


            //修改时间
            cell = row.createCell(5);
            cell.setCellStyle(cellStyleList.get(4));
            cell.setCellValue(dateTimeFormatter.format(category.getUpdateTime()));
        }

        //6. 输出工作簿即可,通知浏览器当前是以文件下载的形式去处理内容。
        response.setHeader("content-disposition","attachment;filename=category.xlsx");
        workbook.write(response.getOutputStream());
    }
    //分类查询
    @GetMapping("/list")
    public R list(int type){
        return categoryService.list(type);
    }

    //导入

    @RequestMapping("importData")
    public R importData(MultipartFile excelFile, HttpSession session) throws IOException {
        Long id= (Long) session.getAttribute("employee");
        Workbook workbook=new XSSFWorkbook(excelFile.getInputStream());
        Sheet sheet=workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 0; i < lastRowNum; i++) {
            Row row=sheet.getRow(i+1);
            Integer  type=(int) row.getCell(1).getNumericCellValue();
            String  name=row.getCell(2).getStringCellValue();
            Integer sort =(int) row.getCell(3).getNumericCellValue();
            Category category=new Category();
            category.setUpdateUser(id);
            category.setCreateUser(id);
            category.setName(name);
            category.setType(type);
            category.setSort(sort);
            category.setUpdateTime(LocalDateTime.now());
            category.setCreateTime(LocalDateTime.now());
            categoryService.insert(category);
        }

        return R.success("成功");
    }

}
